const util = require('../../../../../../../../utils/util.js');
const api = require('../../../../../../../../config/api.js');
const user = require('../../../../../../../../services/user.js');

//获取应用实例
const app = getApp()
const openid = app.globalData.logininfo.openid;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    circleid: '',//圈子id
    groupJoinUserList: [],// 圈子的用户
    searchText: ''//搜索内容
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this = this;
    var circleid = options.circleid;
    _this.setData({
      circleid: circleid
    })
    _this.getGroupJoinUser();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  
  /**
   * 获取分组后的用户信息
   */
  getGroupJoinUser: function () {
    var _this = this;
    if (openid != null && openid != "") {
      // 已登录
      util.post(api.fetchToken()).then(function (res) {
        if (res.access_token != '') {
          return Promise.resolve({
            access_token: res.access_token
          });
        }
      }).then(function (res) {
        var data = {};
        data.openid = openid;
        data.access_token = res.access_token;
        data.circleid = _this.data.circleid;
        data.searchText = _this.data.searchText;
        util.post(api.Home_GetgroupJoinUserApiUrl, data).then(function (res) {
          if (res.code == 200) {
            _this.setData({
              groupJoinUserList: res.result.list
            })
          }
        })
      })
    }
  },

  /**
   * 搜索
   */
  search: function (e) {
    var _this = this;
    var value = e.detail.value;
    _this.setData({
      searchText: value
    })
    _this.getGroupJoinUser();
  },

  /**
   * 进入用户主页
   */
  navigateToPersonhome: function (e) {
    var openid = e.currentTarget.dataset.openid;
    wx.navigateTo({
      url: '../../../../../personhome/personhome?openid=' + openid
    })
  },
})